dojo.provide("lotex.widget.List");

dojo.require("dojo.lang.declare");
dojo.require("dojo.html.style");
dojo.require("dojo.collections.Dictionary");
dojo.require("lotex.table");
dojo.require("lotex.string");
dojo.require("lotex.io");
dojo.require("dojo.html.style");

dojo.declare("lotex.widget.List", null, {
    c1:    "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">" +
                "<tr>" +
                    "<td>" +
                        "<input type=\"checkbox\" value=\"${id}\" name=\"list_cb1\" id=\"list_cb1\"" +
                            "onclick='DocCheckClick(event, this)'></td>" +
                    "<td>" +
                        "<input type=\"checkbox\" name=\"starRpt\" value=\"\" dojotype=\"lotex:Checkbox\"></td>" +
                    "<td style=\"padding-right: 3px\">" +
                        "<a class=\"btn dropdn\" href=\"#\" onClick=\"return ShowDocumentMenu(this, '${0}', true, '');\"" +
                            "style=\"margin: 0; width: 40px;\"><b><b><b>" +
                                "<img src=\"arrow_doc.gif\" width=\"16\" height=\"16\" /></b></b></b></a></td>" +
                    "<td>" +
                        "<b><a href=\"/resources/rpt/${0}\" onClick=\"return rptp.app.openEditWindow('${0}','', '', '', '')\"" +
                            "class=\"app\">${1}</a></b>&nbsp;&nbsp;<a class=\"tagLink\" href=\"/?action=search&searchTag=test\">测试</a></td>" +
                "</tr>" +
            "</table>",
    initializer: function(url) {
        this.dictionary=new dojo.collections.Dictionary();
        this.url = url;
        this.domNode = dojo.html.getElementsByClass("doclist")[0];

        this.loadData();
        // TODO refact
        dojo.event.topic.subscribe("rptListMessageTopic", this, "_handleMessage");
    },

    _handleMessage: function(msg) {
        if(msg=="reflash") this.reflash();
    },

    loadData: function() {
        var self = this;
        var url = rptp.rpt.getRptListURL();
        // TODO 包装json，使具有onJsonError
        lotex.io.json({
            url: url, //"rpt_list.json"],
            error: function(type, data, evt){
                if(type=="JSONParseException")
                    lotex.browser.NavWindowToURL(lotexConfig.baseurl+"/login.jsp");
                else
                    d("404 error! can't load url : " + this.url);
            },
            load: function(type, data, evt) {
                d("success load url : " + this.url);
                self.paintList(data);
            }
        });
    },

    paintList: function(data) {
        var newData = data.length;
        for (var i = 0; i < data.length; i++) {
            var id = data[i][0];
            if(this.dictionary.contains(id)) {newData--;continue;}
            this.appendRow(data[i]);
            this.dictionary.add(id, data[i]);
        }
        if(newData>0) dojo.hostenv.makeWidgets();
        //var parser = new dojo.xml.Parse();
        //var frag = parser.parseElement(this.domNode, null, true);
        //dojo.widget.getParser().createComponents(frag);

        app.message("显示完毕！");
    },

    cleanList: function() {
        var length = this.domNode.rows.length;
        for (var i = 0; i < length; i++) {
            this.domNode.deleteRow(0);
        }
    },
    // 没有删除列表
    reflash: function() {
        app.message("刷新中……");
        //this.cleanList();
        this.loadData();
    },

    deleteRowByRptId: function(rptId) {
        app.message("删除数据中……");

        var tb = this.domNode;
        var length = tb.rows.length;
        for (var i = 1; i < length; i++) {
            var rowRptId = dojo.html.getAttribute(tb.rows[i], "rptId");
            if(rowRptId==rptId) {
                tb.deleteRow(i);
                break;
            }
        }
        d("删除数据行"+i);
    },

    appendRow: function(data) {
        var row=lotex.table.appendRow("doclist");
        var id=data[0];
        row.setAttribute("rptId", id);
        with (row){
            valign="top";

            var oCell=insertCell(0);
            oCell.className="app listLine";
            oCell.innerHTML= lotex.string.sprintf(this.c1, data);

            var oCell=insertCell(1);
            oCell.className="app listLine listLineMore";
            oCell.innerHTML= "&nbsp;";
            var oCell=insertCell(2);
            oCell.className="app listLine listLineMore";
            oCell.innerHTML= "罗涛";
            var oCell=insertCell(3);
            oCell.className="app listLine listLineMore";
            oCell.innerHTML= "&nbsp;";
            var oCell=insertCell(4);
            oCell.className="app listLine listLineMore";
            oCell.innerHTML= "2006-12-01";
        }
    }
});